package com.xc.service.impl;

import com.alibaba.fastjson.JSON;
import com.xc.common.ServerResponse;
import com.xc.dao.MenuMapper;
import com.xc.pojo.Menu;
import com.xc.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Override
    public ServerResponse insertMenu(Map<String,Object> params) throws Exception {
        menuMapper.insertMenu(JSON.parseObject(JSON.toJSONString(params), Menu.class));
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse updateMenu(Map<String,Object> params) {
        menuMapper.updateMenu(JSON.parseObject(JSON.toJSONString(params), Menu.class));
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteMenu(Integer menuId) {
        menuMapper.deleteMenu(menuId);
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse menuList(Map<String,String> params) {
        String title = params.get("title");
        List<Menu> menus = menuMapper.menuList(title);
        menus.forEach( m -> {
            m.setChildren(menuMapper.menuListByPid(m.getMenuId()));
        });
        return ServerResponse.createBySuccess(menus);
    }


}
